    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.business.support;

import cz.cvut.fel.taskpro.business.isupport.IBusinessRuleValidator;
import cz.cvut.fel.taskpro.data.entity.*;
import cz.cvut.fel.taskpro.data.imanager.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Singleton;
import javax.inject.Inject;

/**
 * This class implement #{@link IBusinessRuleValidator} interface
 * @author Tom
 * @version 1.0.0. beta
 */
@Singleton
public class BusinessRuleValidator implements Serializable, IBusinessRuleValidator {

    private final int MIN_EMAIL_LENGHT = 4;
    private final int MIN_LOGIN_LENGTH = 2;
    @Inject
    private IAssignmentData assignmentData;
    @Inject
    private ITeamData teamData;
    @Inject
    private ISubjectData subjectData;
    @Inject
    private IGroupData groupData;
    @Inject
    private IEnviromentData enviromentData;
    @Inject
    private IUserData userData;

    public BusinessRuleValidator() {
    }

    @Override
    public void validate(TaskProUser taskProUser) throws BusinessRuleException {
        if (taskProUser == null) {
            throw new BusinessRuleException(1001, "The user was not initialized.");
        }
        if ((taskProUser.getEmail().length() < MIN_EMAIL_LENGHT)) {
            throw new BusinessRuleException(1001, "The e-mail is too short. Minimal lenght is " + MIN_EMAIL_LENGHT + ".");
        }
        if (taskProUser.getLogin().length() < MIN_LOGIN_LENGTH) {
            throw new BusinessRuleException(1001, "The login is too short. Minimal lenght is " + MIN_LOGIN_LENGTH + ".");
        }

    }

    @Override
    public void validateAdding(TaskProUser taskProUser) throws BusinessRuleException {
        validate(taskProUser);
        TaskProUser findUser = userData.findByLogin(taskProUser.getLogin());
        if (findUser != null) {
            throw new BusinessRuleException(1002, "User with login: " + taskProUser.getLogin() + " is already in database.");
        }
        findUser = userData.findByEmail(taskProUser.getEmail());
        if (findUser != null) {
            throw new BusinessRuleException(1002, "User with e-mail: " + taskProUser.getEmail() + " is already in database.");
        }
    }

    @Override
    public void validateSubmitTask(AssignmentSubmit assignmentSubmit) throws BusinessRuleException {
        if (assignmentSubmit == null) {
            throw new BusinessRuleException(2001, "No Assignment submit was initializate.");
        }
        boolean isClosed = false;
        if (assignmentSubmit.getAssignment().getType().equals(Assignment.Type.TEAM)) {
            GroupTaskPro submittedUserGroup=groupData.getGroupBySubjectAndStudent(assignmentSubmit.getAssignment().getSubject(),assignmentSubmit.getStudent());
           Team submittedUserSubjectTeam=teamData.getTeamByStudentAndGroup(assignmentSubmit.getStudent(), submittedUserGroup);
            isClosed = assignmentData.isAssignmentSubmitExist(assignmentSubmit.getAssignment(), assignmentSubmit.getStudent(), AssignmentSubmit.State.ACCEPTED,submittedUserSubjectTeam);
        } else {
            isClosed = assignmentData.isAssignmentSubmitExist(assignmentSubmit.getAssignment(), assignmentSubmit.getStudent(), AssignmentSubmit.State.ACCEPTED,null);
        }
        if (isClosed) {
            throw new BusinessRuleException(2003, "This assignment is already CLOSED.");
        }
        if (assignmentSubmit.getAssignment().getDeadline().before(Maintance.getTodayDate())) {
            throw new BusinessRuleException(2003, "The assignment deadline has expired.");
        }
    }

    @Override
    public void validate(Version version) throws BusinessRuleException {
        if (version == null) {
            throw new BusinessRuleException(3001, "No files was added to this task.");
        }
        if (version.getmVersionFiles() == null) {
            throw new BusinessRuleException(3001, "No files was added to this task.");
        }
        if (version.getmVersionFiles().isEmpty()) {
            throw new BusinessRuleException(3001, "No files was added to this task.");
        }
        if (version.getAssignmentSubmit() == null) {
            throw new BusinessRuleException(3001, "No Assignment submit was initializate.");
        }

    }

    @Override
    public void validate(List<Subject> subjects) throws BusinessRuleException {
        if (subjects == null) {
            throw new BusinessRuleException(4001, "No subjects were choosen.");
        }
        if (subjects.isEmpty()) {
            throw new BusinessRuleException(4001, "No subjects were choosen.");
        }

    }

    @Override
    public void validateStudents(List<Student> mStudents) throws BusinessRuleException {
        if (mStudents == null) {
            throw new BusinessRuleException(1001, "No students were choosen.");
        }
        if (mStudents.isEmpty()) {
            throw new BusinessRuleException(1001, "No students were choosen.");
        }
    }

    @Override
    public void validateGroups(List<GroupTaskPro> group) throws BusinessRuleException {
        if (group == null) {
            throw new BusinessRuleException(5001, "No groups were choosen.");
        }
        if (group.isEmpty()) {
            throw new BusinessRuleException(5001, "No groups were choosen.");
        }
    }

    @Override
    public void validateAddTeam(Team newTeam) throws BusinessRuleException {
        validateTeam(newTeam);
        List<Team> teamsByCriteria = teamData.getTeamByContext(newTeam.getName(), newTeam.getGroup());
        if (!teamsByCriteria.isEmpty()) {
            throw new BusinessRuleException(6002, "The team named as: " + newTeam.getName() + " is already created in group: " + newTeam.getGroup().getCode());
        }
    }

    @Override
    public void validateTeam(Team newTeam) throws BusinessRuleException {
        if (newTeam == null) {
            throw new BusinessRuleException(6001, "No team to create");
        }
        if (newTeam.getmStudents().isEmpty()) {
            throw new BusinessRuleException(1001, "No mStudents were added to this team");
        }
        if (newTeam.getGroup() == null) {
            throw new BusinessRuleException(5001, "No groups were choosen.");
        }
        List<Student> studentsInTeam = teamData.getStudentsInTeamByGroup(newTeam.getGroup());
        Iterator it = newTeam.getmStudents().iterator();
        while (it.hasNext()) {
            Student currStudent = (Student) it.next();
            if (studentsInTeam.contains(currStudent)) {
                throw new BusinessRuleException(6002, "Student: " + currStudent.getLogin() + " is already in team in this group.");
            }
        }
    }

    @Override
    public void validateRate(AssignmentSubmit assignmentSubmit) throws BusinessRuleException {
        if (assignmentSubmit == null) {
            throw new BusinessRuleException(2001, "No Assignment submit was initializate.");
        }
        if ((assignmentSubmit.getEvaluation() == null) && assignmentSubmit.getGrade() == 0) {
            throw new BusinessRuleException(2001, "The evaluation or grade was not modify.");
        }
        int maxScore = assignmentSubmit.getAssignment().getMaxScore();
        if (assignmentSubmit.getGrade() > maxScore) {
            throw new BusinessRuleException(2002, "The grade is greater than max score. Max grade is: " + maxScore);
        }
        if(assignmentSubmit.getGrade()<0){
            throw new BusinessRuleException(2004, "The grade could not be less than zero");
        }
        if (assignmentSubmit.getStateOfTask() == AssignmentSubmit.State.SUBMITTED) {
            throw new BusinessRuleException(2003, "The state couldnot be Submitted after rate");
        }
    }

    @Override
    public void validateAdding(GroupTaskPro groupToAdd) throws BusinessRuleException {
        if (groupToAdd == null) {
            throw new BusinessRuleException(6000, "The group was not initialized");
        }
        if (groupToAdd.getCode() == null) {
            throw new BusinessRuleException(6001, "The group code is missing");
        }
        if (groupToAdd.getSubject() == null) {
            throw new BusinessRuleException(6001, "There are no subject selected in group " + groupToAdd.getCode() + ". At least"
                    + " one subject must be choosen.");
        }
        Date startTime = groupToAdd.getStartTime();
        Date stopTime = groupToAdd.getStopTime();
        if (startTime != null) {
            if (stopTime == null) {
                throw new BusinessRuleException(6005, "The stop time must be specify if start time is specify.");
            }
            if ((startTime.after(stopTime)) || (startTime.equals(stopTime))) {
                throw new BusinessRuleException(6005, "The stoptime must be different or greater than startTime.");
            }
        }
        if (stopTime != null) {
            if (startTime == null) {
                throw new BusinessRuleException(6005, "The start time must be specify if stop time is specify.");
            }
        }
        List<GroupTaskPro> groupInDb = groupData.getGroupBySubjectAndCode(groupToAdd.getSubject(), groupToAdd.getCode());
        if (!groupInDb.isEmpty()) {
            throw new BusinessRuleException(6002, "There is already exist the group with code: " + groupToAdd.getCode() + " in "
                    + "subject " + groupToAdd.getSubject().getCode() + ".");
        }
        if (groupToAdd.getMStudents() == null) {
            throw new BusinessRuleException(6001, "There are no mStudents in group " + groupToAdd.getCode() + ". At least"
                    + " one student must be choosen.");
        } else if (groupToAdd.getMStudents().isEmpty()) {
            throw new BusinessRuleException(6001, "There are no mStudents in group " + groupToAdd.getCode() + ". At least"
                    + " one student must be choosen.");
        }
        if (groupToAdd.getKantor() == null) {
            throw new BusinessRuleException(6001, "There is no kantor in group " + groupToAdd.getCode());
        }
        List<Student> studentsInSubject = subjectData.getStudentsInSubject(groupToAdd.getSubject());
        if (!studentsInSubject.isEmpty()) {
            Iterator stI = groupToAdd.getMStudents().iterator();
            while (stI.hasNext()) {
                Student currStudent = (Student) stI.next();
                if (studentsInSubject.contains(currStudent)) {
                    throw new BusinessRuleException(6002, "The student with login " + currStudent.getLogin() + " is already"
                            + "registred in this subject.");
                }
            }
        }
    }

    @Override
    public void validate(Assignment assignment) throws BusinessRuleException {
        if (assignment == null) {
            throw new BusinessRuleException(7001, 
                    "No assignment was initialized.");
        }
        if (assignment.getMaxScore() <= 0) {
            throw new BusinessRuleException(7003, "The max points which can "
                    + "students earn on this task must be greather than 0."
                    + "The value: " + assignment.getMaxScore() + " "
                    + "is not permitted.");
        }
        Date currDate = Maintance.getTodayDate();
        if (assignment.getDeadline().before(currDate)) {
            throw new BusinessRuleException(7003, "The deadline of date: " 
                    +assignment.getDeadline() + " is expired.");
        }
        if(assignment.getDateOfStart().after(assignment.getDeadline())){
              throw new BusinessRuleException(7003, "The date of start: " 
                      +assignment.getDeadline() + " could not be after "
                      + "deadline, which is: "+assignment.getDeadline()+" .");
        }
        if (assignment.getTemplate() == null) {
            throw new BusinessRuleException(7001, "No template of"
                    + " task  was initialized.");
        }
        if (assignment.getType() == Assignment.Type.GROUP) {
            if (assignment.getmGroups() == null) {
                throw new BusinessRuleException(7001, "The group was"
                        + " not initialized");
            } else if (assignment.getmGroups().isEmpty()) {
                throw new BusinessRuleException(7001, 
                        "The group was not initialized");
            }
        } else if (assignment.getType() == Assignment.Type.STUDENT) {
            if (assignment.getmStudents() == null) {
                throw new BusinessRuleException(7001, "The students"
                        + " was not initialized");
            } else if (assignment.getmStudents().isEmpty()) {
                throw new BusinessRuleException(7001, "The students "
                        + "was not initialized");
            }
        } else if (assignment.getType() == Assignment.Type.TEAM) {
            if (assignment.getmTeams() == null) {
                throw new BusinessRuleException(7001, "The team"
                        + " was not initialized");
            } else if (assignment.getmTeams().isEmpty()) {
                throw new BusinessRuleException(7001, "The team"
                        + " was not initialized");
            }
        } else {
            throw new BusinessRuleException(7005, "Type of task"
                    + " could not be recognized.");
        }
        if (assignment.getSubject() == null) {
            throw new BusinessRuleException(7000, "The subject"
                    + " was not initialized.");
        }
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public void validate(Template template) throws BusinessRuleException {
        if (template == null) {
            throw new BusinessRuleException(8000, "The template was not initialized");
        }
        if (template.getTemplateFiles() == null) {
            if ((template.getDescription().isEmpty())) {
                throw new BusinessRuleException(8001, "No files and description was assigned"); 
            }
        }
        if (template.getDescription().isEmpty()) {
            throw new BusinessRuleException(8001, "No description of task was assigned");
        }
        if (template.getName().isEmpty()) {
            {
                throw new BusinessRuleException(8001, "No name of template was setted up.");
            }
        }
    }

    @Override
    public void validateAdding(List<TaskProUser> users) throws BusinessRuleException {
        if (users == null) {
            throw new BusinessRuleException(1001, "The user was not initialized.");
        }
        Iterator userIt = users.iterator();
        while (userIt.hasNext()) {
            TaskProUser userToValidate = (TaskProUser) userIt.next();
            validateAdding(userToValidate);
        }
    }

    @Override
    public void validate(Subject subject) throws BusinessRuleException {
        if (subject == null) {
            throw new BusinessRuleException(4001, "No subjects were choosen.");
        }
    }

    @Override
    public void validateAding(Subject newSubject) throws BusinessRuleException {
       validate(newSubject);
        if (newSubject.getCode() == null) {
            throw new BusinessRuleException(4002, "Subject code could not be empty");
        }
        Subject subjectInDb = subjectData.getSubjectByCode(newSubject.getCode());
        if (subjectInDb != null) {
            throw new BusinessRuleException(4003, "Subject code " + newSubject.getCode() + " is already in database.");
        }
    }

    @Override
    public void validateModification(TaskProUser user) throws BusinessRuleException {
        validate(user);
        TaskProUser findUserMail = userData.findByEmail(user.getEmail());
        TaskProUser findByLogin = userData.findByLogin(user.getLogin());
        if ((findByLogin == null) && (findUserMail == null)) {
            return;
        }
        if (findByLogin != null) {
            if (!user.equals(findByLogin)) {
                throw new BusinessRuleException(1002, "User with login: " + findByLogin.getLogin() + " is already in database.");
            }
        }
        if (findUserMail != null) {
            if (!user.equals(findUserMail)) {
                throw new BusinessRuleException(1002, "User with E-mail: " + findUserMail.getEmail() + " is already in database. and belong to user with username: " + findUserMail.getLogin());
            }
        }
    }

    @Override
    public void validateAddingEnvriroment(List<SystemEnviroment> enviroments) throws BusinessRuleException {
        if (enviroments == null) {
            throw new BusinessRuleException(1200, "The enviroments was  not be initialized");
        }
        if (enviroments.isEmpty()) {
            throw new BusinessRuleException(1200, "The enviroments was  not be initialized");
        }
        Iterator enIt = enviroments.iterator();
        while (enIt.hasNext()) {
            SystemEnviroment currEnviroment = (SystemEnviroment) enIt.next();
            this.validateAddingEnvriroment(currEnviroment);
        }
    }

    @Override
    public void validateAddingEnvriroment(SystemEnviroment enviroment) throws BusinessRuleException {
        if (enviroment == null) {
            throw new BusinessRuleException(1200, "The enviroment was  not be initialized");
        }
        if (enviroment.getKeyOfSetting() == null) {
            throw new BusinessRuleException(1201, "The enviroment key could not be empty");
        }
        if (enviroment.getKeyOfSetting().isEmpty()) {
            throw new BusinessRuleException(1201, "The enviroment key  could not be empty");
        }
        if (enviroment.getValueOfSetting() == null) {
            throw new BusinessRuleException(1201, "The enviroment value for key: " + enviroment.getKeyOfSetting() + " could not be empty");
        }
        if (enviroment.getValueOfSetting().isEmpty()) {
            throw new BusinessRuleException(1201, "The enviroment value for key: " + enviroment.getKeyOfSetting() + "  could not be empty");
        }
        List<SystemEnviroment> enviromentInDb = enviromentData.getEnviroment(enviroment.getKeyOfSetting());
        if (!enviromentInDb.isEmpty()) {
            throw new BusinessRuleException(1202, "The enviroment " + enviroment.getKeyOfSetting() + " is already specified in DB");
        }
    }

    @Override
    public void validateModification(List<SystemEnviroment> enviroments) throws BusinessRuleException {
        if (enviroments == null) {
            throw new BusinessRuleException(1200, "The enviroment was  not be initialized");
        }
        Iterator envIt = enviroments.iterator();
        List<String> keys = new ArrayList<String>();
        while (envIt.hasNext()) {
            SystemEnviroment currEnv = (SystemEnviroment) envIt.next();
            if (currEnv.getKeyOfSetting() == null) {
                throw new BusinessRuleException(1201, "The enviroment key could not be empty");
            }
            if (currEnv.getKeyOfSetting().isEmpty()) {
                throw new BusinessRuleException(1201, "The enviroment key  could not be empty");
            }
            if (currEnv.getValueOfSetting() == null) {
                throw new BusinessRuleException(1201, "The enviroment value for key: " + currEnv.getKeyOfSetting() + " could not be empty");
            }
            if (currEnv.getValueOfSetting().isEmpty()) {
                throw new BusinessRuleException(1201, "The enviroment value for key: " + currEnv.getKeyOfSetting() + "  could not be empty");
            }
            if (keys.contains(currEnv.getKeyOfSetting())) {
                throw new BusinessRuleException(1202, "The enviroment " + currEnv.getKeyOfSetting() + " is multiple specified.");
            }
            keys.add(currEnv.getKeyOfSetting());
        }
    }

    @Override
    public void validateRemove(Team teamToRemove) throws BusinessRuleException {
        if (teamToRemove == null) {
            throw new BusinessRuleException(6001, "No team to create");
        }
        if (teamToRemove.getmStudents().isEmpty()) {
            throw new BusinessRuleException(1001, "No mStudents were added to this team");
        }
        if (teamToRemove.getGroup() == null) {
            throw new BusinessRuleException(5001, "No groups were choosen.");
        }
        List<AssignmentSubmit> submitedAssignment = assignmentData.getAssignmentSubmitByTeam(teamToRemove);
        if (!submitedAssignment.isEmpty()) {
            throw new BusinessRuleException(5009, "Team " + teamToRemove.getName() + " has already submit assignments and could not be removed.");
        }
    }

    @Override
    public void validateRemove(List<Assignment> assignmentsToRemove) throws BusinessRuleException {
        if (assignmentsToRemove == null) {
            throw new BusinessRuleException(7001, "No assignment was initialized.");
        }
        Iterator asIt = assignmentsToRemove.iterator();
        while (asIt.hasNext()) {
            validateRemove((Assignment) asIt.next());
        }
    }

    @Override
    public void validateRemove(Assignment assignmentsToRemove) throws BusinessRuleException {
        if (assignmentsToRemove == null) {
            throw new BusinessRuleException(7001, "No assignment was initialized.");
        }
        List<AssignmentSubmit> assignmentSubmits = assignmentData.getAssignmentSubmitsByAssignment(assignmentsToRemove);
        if (!assignmentSubmits.isEmpty()) {
            String plural = "there are assignments which were already submitted.";
            if (assignmentSubmits.size() == 1) {
                plural = "there is assignment which was already submitted.";
            }
            throw new BusinessRuleException(7009, "Assignment " + assignmentsToRemove.getTemplate().getName()
                    + " assigned in subject" + assignmentsToRemove.getSubject().getCode() + " could not be removed,"
                    + "because " + plural);
        }
    }

    @Override
    public void validateRemove(Team teamWithStudents, List<Student> studentsToRemove) throws BusinessRuleException {
        if (teamWithStudents == null) {
            throw new BusinessRuleException(6001, "No team to create");
        }
        if (studentsToRemove == null) {
            throw new BusinessRuleException(1001, "The students was not initialized.");
        }
        if (studentsToRemove.isEmpty()) {
            throw new BusinessRuleException(1001, "The students was not initialized.");
        }

    }

    @Override
    public void validate(GroupTaskPro group) throws BusinessRuleException {
        if (group == null) {
            throw new BusinessRuleException(5001, "No groups were choosen.");
        }
    }

    @Override
    public void validateRemove(GroupTaskPro groupToDelete) throws BusinessRuleException {
        if (groupToDelete == null) {
            throw new BusinessRuleException(6000, "Group to delete was not initialized.");
        }
        List<Subject> subjectInGroup=new ArrayList<Subject>();
        subjectInGroup.add(groupToDelete.getSubject());
        List<AssignmentSubmit> kantorAssingment = assignmentData.getAssignmentSubmitByContext(groupToDelete.getMStudents(), null, subjectInGroup);
        if (!kantorAssingment.isEmpty()) {
            throw new BusinessRuleException(6009, "The group teached by " + groupToDelete.getKantor().getName() + groupToDelete.getKantor().getSurname() + " "
                    + "has already submitted assigned task to group so this group could not be deleted.");
        }
        List<Team> teamInGroup=groupToDelete.getmTeams();
        Iterator teamIt=teamInGroup.iterator();
        while(teamIt.hasNext()){
            Team currTeam=(Team) teamIt.next();
            List<AssignmentSubmit> submittedTeamAssingment=assignmentData.getAssignmentSubmitByTeam(currTeam);
            if(!submittedTeamAssingment.isEmpty()){
                throw new BusinessRuleException(6009, "The group teached by " + groupToDelete.getKantor().getName() + groupToDelete.getKantor().getSurname() + " "
                    + "has already submitted assigned task to group so this group could not be deleted.");
            } 
        }
    }

    @Override
    public void validateModification(GroupTaskPro oldGroup, GroupTaskPro updatedGroup, List<Student> studentsToRemove) throws BusinessRuleException {
        validate(oldGroup);
        validate(updatedGroup);
        if (!updatedGroup.getCode().equals(oldGroup.getCode())) {
            List<GroupTaskPro> foundedGroup = groupData.getGroupBySubjectAndCode(updatedGroup.getSubject(), updatedGroup.getCode());
            if (!foundedGroup.isEmpty()) {
                throw new BusinessRuleException(6001, "Group with code " + updatedGroup.getCode() + " is already exist");
            }
        }
        Date startTime = updatedGroup.getStartTime();
        Date stopTime = updatedGroup.getStopTime();
        if (startTime != null) {
            if (stopTime == null) {
                throw new BusinessRuleException(6005, "The stop time must be specify if start time is specify.");
            }
            if ((startTime.after(stopTime)) || (startTime.equals(stopTime))) {
                throw new BusinessRuleException(6005, "The stoptime must be different or greater than startTime.");
            }
        }
        if (stopTime != null) {
            if (startTime == null) {
                throw new BusinessRuleException(6005, "The start time must be specify if stop time is specify.");
            }
        }
        if (updatedGroup.getMStudents() == null) {
            throw new BusinessRuleException(6001, "There are no mStudents in group " + updatedGroup.getCode() + ". At least"
                    + " one student must be choosen.");
        } else if (updatedGroup.getMStudents().isEmpty()) {
            throw new BusinessRuleException(6001, "There are no mStudents in group " + updatedGroup.getCode() + ". At least"
                    + " one student must be choosen.");
        }
        if (updatedGroup.getKantor() == null) {
            throw new BusinessRuleException(6001, "There is no kantor in group " + updatedGroup.getCode());
        }
        Iterator studentInGroupIt = updatedGroup.getMStudents().iterator();
        while (studentInGroupIt.hasNext()) {
            Student currStudent = (Student) studentInGroupIt.next();
            GroupTaskPro studentGroup = groupData.getGroupBySubjectAndStudent(updatedGroup.getSubject(), currStudent);
            if (studentGroup != null) {
                if (!studentGroup.equals(oldGroup)) {
                    throw new BusinessRuleException(6002, "The student with login " + currStudent.getLogin() + " is already"
                            + "registred in this subject.");
                }
            }

        }
        if (studentsToRemove == null) {
        } else if (studentsToRemove.isEmpty()) {
        } else {
            Iterator studentIt = studentsToRemove.iterator();
            while (studentIt.hasNext()) {
                Student currStudent = (Student) studentIt.next();
                List<Team> studentTeam = currStudent.getTeam();
                Team teamGroup = teamData.getTeamByStudentAndGroup(currStudent, oldGroup);
                if (studentTeam.contains(teamGroup)) {
                    throw new BusinessRuleException(6008, "The student with login " + currStudent.getLogin() + " is member"
                            + " of team " + teamGroup.getName() + " so he could not be removed from group " + oldGroup.getCode());
                }
                List<Assignment> assignmentSubmitByStudent = assignmentData.getAssignmentSubByContext(currStudent, null, false);
                if (!assignmentSubmitByStudent.isEmpty()) {
                    throw new BusinessRuleException(6002, "The student with login " + currStudent.getLogin() + " has already"
                            + " submitted any assignments.");
                }
            }
        }
    }

    @Override
    public void validateUpdate(VersionFile versionFile) throws BusinessRuleException {
        if (versionFile == null) {
            throw new BusinessRuleException(11002, "The version file has not been initialized.");
        }
    }

    @Override
    public void validateUpdate(Template templateToUpdate) throws BusinessRuleException {
        validate(templateToUpdate);
        if (templateToUpdate.getId() == null) {
            throw new BusinessRuleException(7001, "No template  was initialized.");
        }
    }

    @Override
    public void validateRemove(Template templateToremove) throws BusinessRuleException {
        validate(templateToremove);
        List<Assignment> assginedTask=assignmentData.getAssignmentByTemplate(templateToremove);
        if(!assginedTask.isEmpty()){
             throw new BusinessRuleException(8006, "This template is assigned to task, and it could not be removed.");
        }      
    }
}
